package leecode

func maxScore(cardPoints []int, k int) int {
	preSum := 0 //从前面拿和
	for i := 0; i < k; i++ {
		preSum += cardPoints[i]
	}
	result := preSum
	//从后面拿k张的和
	sufSum := 0
	for i := 0; i < k; i++ {
		preSum -= cardPoints[k-i-1]               //从前减
		sufSum += cardPoints[len(cardPoints)-1-i] //从后加
		if result < preSum+sufSum {
			result = preSum + sufSum
		}
	}
	return result
}
